### Create ridgeplot for the threat variables

library(haven)
library(tidyverse)
library(ggplot2)
library(ggridges)
library(viridis)
library(extrafont)
library(Cairo)
loadfonts(device = "win")
windowsFonts(Times = windowsFont("TT Times New Roman"))

#Figure 1
data <- read_stata("DATA_RPlot_Fig1.dta")

data <- data %>% 
  mutate(country = labelled::to_factor(QCOUNTRY),
         country = fct_rev(country)) %>%
  mutate_if(is.numeric, labelled::remove_val_labels) %>% 
  drop_na() %>% 
  dplyr::select(country, threat_index = threat_ind2)

# reorder based on mean 
data$country = reorder(data$country, data$threat_index, mean)

fig1 <- ggplot(data, aes(x = threat_index, y = country)) +
  geom_density_ridges(scale = 3, size = 0.8) + 
  labs(title = '') +
  theme(
    legend.position="none",
    panel.spacing = unit(0.1, "lines")
  ) +
  labs(x = "", y = "") + 
  geom_vline(xintercept = 0.5, linetype="solid", size = 1) + 
  geom_vline(xintercept = mean(data$threat_index), linetype="dashed", size = 1) + 
  scale_x_continuous(expand = c(0.015, 0.015),
                     breaks = c(0,0.25,0.50,0.75,1), 
                     labels = c("0.00 \n no threat", "0.25", "0.50", "0.75", "1.00 \n critical threat")) + 
  theme_bw(base_size = 28, base_family='Times New Roman') + 
  theme(plot.margin = margin(t = -10,  # Top margin
                             r = 50,  # Right margin
                             b = -10,  # Bottom margin
                             l = -10)) # Left margin)


ggsave(plot = fig1, "Figure_1.png", type = "cairo-png")

#Figure 2


data2 <- read_stata("DATA_RPlot_Fig2.dta")

data2 <- data2 %>% 
  mutate(country = labelled::to_factor(QCOUNTRY),
         country = fct_rev(country)) %>%
  mutate_if(is.numeric, labelled::remove_val_labels) %>% 
  drop_na() %>% 
  mutate(csdp_wouldlike = (Q24-6)/(-5)) %>% 
  dplyr::select(country, csdp_wouldlike) %>% 
  group_by(country) %>% 
  dplyr::mutate(avg_csdp_wouldlike = mean(csdp_wouldlike)) %>% 
  ungroup()

data2Summary <- data2 %>% 
  group_by(country) %>% 
  dplyr::summarise(avg_csdp_wouldlike = mean(csdp_wouldlike), 
                   uci_csdp_wouldlike = Rmisc::CI(csdp_wouldlike)[1], 
                   lci_csdp_wouldlike = Rmisc::CI(csdp_wouldlike)[3])

pd <- position_dodge(width = 0.2)

#Mean + CI
fig2 <- ggplot(data2Summary, aes(x=reorder(country, avg_csdp_wouldlike), y = avg_csdp_wouldlike, group = country)) +
  geom_point(position = pd, size = 2) +
  geom_errorbar(aes(ymin=lci_csdp_wouldlike, ymax=uci_csdp_wouldlike, group=country), width=.4, position=pd, size = 0.9) + 
  geom_hline(yintercept = mean(data2Summary$avg_csdp_wouldlike), linetype="dashed", size = 1) + 
  geom_hline(yintercept = 0.5, linetype="solid", size = 1) +
  labs(title = '') +
  coord_flip() + 
  labs(x = "", y = "") + 
  scale_y_continuous(expand = c(0.025, 0.025),
                     limits = c(0,1),
                     breaks = c(0,0.25,0.50,0.75,1), 
                     labels = c("0.00 \n not integrated", "0.25", "0.50", "0.75", "1.00 \n fully integrated")) + 
  theme_bw(base_size = 28, base_family='Times New Roman') + 
  theme(
    legend.position="none",
    plot.margin = margin(t = -10,  # Top margin
                         r = 50,  # Right margin
                         b = -10,  # Bottom margin
                         l = -10) # Left margin)
  )

ggsave(plot = fig2, "Figure_2.png", type = "cairo-png")


#Boxplot + Mean
ggplot(data2, aes(x=reorder(country, avg_csdp_wouldlike), y = csdp_wouldlike, group = country)) +
  geom_boxplot(fill = "lightgrey", notch = F) +
  geom_point(data = data2Summary, aes(y = avg_csdp_wouldlike), position = pd, shape = 3) +
  geom_hline(yintercept = mean(data2$csdp_wouldlike), linetype="dashed") + 
  geom_hline(yintercept = 0.5, linetype="solid") +
  labs(title = '') +
  coord_flip() + 
  labs(x = "", y = "") + 
  scale_y_continuous(expand = c(0.025, 0.025),
                     limits = c(0,1),
                     breaks = c(0,0.25,0.50,0.75,1), 
                     labels = c("0.00 \n not integrated", "0.25", "0.50", "0.75", "1.00 \n fully integrated")) + 
  theme(
    legend.position="none"
  ) +
  theme_bw(base_size = 24, base_family='Times New Roman')



#Arrange by threat 
threatSummary <- data %>% 
  group_by(country) %>% 
  summarise(avg_threat_index = mean(threat_index))

data2Summary_threat <- data2Summary %>% 
  left_join(threatSummary, by = "country")

ggplot(data2Summary_threat, aes(x=reorder(country, avg_threat_index), y = avg_csdp_wouldlike, group = country)) +
  geom_point(position = pd) +
  geom_errorbar(aes(ymin=lci_csdp_wouldlike, ymax=uci_csdp_wouldlike, group=country), width=.1, position=pd) + 
  geom_hline(yintercept = mean(data2Summary_threat$avg_csdp_wouldlike), linetype="dashed") + 
  labs(title = '') +
  coord_flip() + 
  labs(x = "", y = "") + 
  scale_y_continuous(limits = c(0,1),
                     breaks = c(0,0.25,0.50,0.75,1), 
                     labels = c("0.00 \n not integrated", "0.25", "0.50", "0.75", "1.00 \n fully integrated")) + 
  theme(
    legend.position="none"
  ) +
  theme_bw(base_size = 16, base_family='Times New Roman')
